package com.hanson.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.GroupGoodsBean;
import com.hanson.service.GroupGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("/admin/groupGoods")
public class GroupGoodsController {
    @Autowired
    private GroupGoodsService groupGoodsService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String login(ModelMap modelMap) {
        return "admin/groupGoods/index";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(ModelMap modelMap, String id) {
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("data", groupGoodsService.selectById(Integer.parseInt(id)));
        }
        return "admin/groupGoods/modify";
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<GroupGoodsBean> listData(int limit, int page, GroupGoodsBean groupGoodsBean) {
        Page<GroupGoodsBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(groupGoodsService.listGroupGoods(pageInfo, groupGoodsBean));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage add(GroupGoodsBean groupGoodsBean) {
        ResultMessage resultMessage = new ResultMessage();
        groupGoodsService.add(groupGoodsBean);
        resultMessage.setMsg("新增组团信息成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modifyData(GroupGoodsBean groupGoodsBean) {
        ResultMessage resultMessage = new ResultMessage();
        groupGoodsService.modify(groupGoodsBean);
        resultMessage.setMsg("修改组团信息成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(String id) {
        ResultMessage resultMessage = new ResultMessage();
        groupGoodsService.deleteById(Integer.parseInt(id));
        resultMessage.setMsg("删除组团成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/updateIsShow/{isShow}/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage updateIsShow(@PathVariable String isShow, @PathVariable String id) {
        ResultMessage resultMessage = new ResultMessage();
        groupGoodsService.updateIsShow(Integer.parseInt(isShow), Integer.parseInt(id));
        resultMessage.setMsg("修改状态成功！");
        return resultMessage;
    }
}
